/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import com.kcp.ko.fmtnmlib.service.search.SimpleSearch;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.*;
import org.apache.tapestry.bean.EvenOdd;
import org.braintrain.domain.Module;
import org.braintrain.domain.User;

import java.util.Collections;
import java.util.List;

/**
 * Tapestry EditUser page.  Allows the user to edit a given or create a new user.
 * 
 * @author John Ericksen
 *
 */
public abstract class EditUser extends BrainTrainSecuredBasePage {

    @Asset("/images/undo.gif")
    public abstract IAsset getBackImage();
    
    @InjectPage("UserAdmin")
    public abstract UserAdmin getUserAdminPage();
    
    //loop variables
    public abstract Long getUserId();
    public abstract void setUserId(Long id);
    
    public abstract Module getModule();
    public abstract void setModule(Module m);
    
    @Bean
    public abstract EvenOdd getEvenOdd();
    
    @Persist
    @InitialValue("new org.braintrain.domain.User()")
    public abstract User getNewUser();
    public abstract void setNewUser(User m);

    @Persist("client")
    public abstract Long getEditableKey();
    public abstract void setEditableKey(Long key);

    /**
     * Returns the current content.  if the page is rewinding, copy off an editable
     * version of the content to allow for editing
     *
     * @return
     */
    public User getUser(){
        return getEditingASO().getEditable(User.class, getEditableKey(), getRequestCycle().isRewinding(), getNewUser());
    }
    
    /**
     * Returns a list of all the available modules
     * 
     * @return
     */
    public List<Module> getModuleList(){
        List<Module> modules = getGenericService().find(SimpleSearch.simpleObjectSearch(Module.class, "active", true));
        
        Collections.sort(modules);
        
        return modules;
    }
    
    /**
     * Returns the cardinality of a user's access to a given module (modules are looped over)
     * @return
     */
    public boolean getModuleAccess()
    {
        return getUser().getModuleAccess().contains(getModule());
    }
    
    /**
     * Sets access to a given module (modules are looped over)
     * @param value
     */
    public void setModuleAccess(boolean value)
    {
        if(value)
        {
            getUser().getModuleAccess().add(getModule());
        }
        else
        {
            getUser().getModuleAccess().remove(getModule());
        }
    }

    /**
     * Initialize the current page with a given user (edit mode)
     * 
     * @param cycle
     * @param userId
     */
    public void activate(IRequestCycle cycle, Long userId)
    {
        setUserId(userId);
        cycle.activate(this);
    }
    
    /**
     * Initialize the current page with a given user (create mode)
     * 
     * @param cycle
     */
    public void activate(IRequestCycle cycle)
    {
        setUserId(null);
        cycle.activate(this);
    }
    
    /**
     * Saves the current user
     * 
     * @param cycle
     */
    public void saveSubmit(IRequestCycle cycle)
    {
        if(cycle.isRewinding())
        {
            getGenericService().saveOrUpdate(getUser());
            
            cycle.activate(getUserAdminPage());
        }
        //todo:redirect
    }
    
    /**
     * Cancels any pending changes
     * @param cycle
     */
    public void cancelSubmit(IRequestCycle cycle)
    {
        cycle.activate(getUserAdminPage());
        //todo:redirect
    }
    
    /**
     * Returns the user's access to this page
     */
    public boolean isUserAccessGranted()
    {
        return isAdmin();
    }
}
